package com.yanqu.road.server.cmd.activity.tongbing;

import com.yanqu.road.entity.activity.tongbing.config.TongBingConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tongbing.TongBingModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_TONG_BING_EXCHANGE_SKIN, desc = "统兵演武兑换服装")
public class TongBingExchangeSkinCmd extends AbstractCommand {

    private int skinId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        TongBingProto.TongBingExchangeSkinReqMsg reqMsg = TongBingProto.TongBingExchangeSkinReqMsg.parseFrom(packet.getBytes());
        skinId = reqMsg.getSkinId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        TongBingConfig config = TongBingMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        ret = player.getModule(TongBingModule.class).exchangeSkin(skinId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            TongBingProto.TongBingExchangeSkinRespMsg.Builder respMsg = TongBingProto.TongBingExchangeSkinRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_TONG_BING_EXCHANGE_SKIN, respMsg);
        }
    }
}
